package edu.ccut.saturn.basecomponent.database.impl.db2.handler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.impl.AbstractSQLHandler;
import edu.ccut.saturn.basecomponent.database.resultset.ResultSetSetUtils;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.basecomponent.databasecomponent.Messages;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnData;

public class DB2SelectLOBSQLHandler extends AbstractSQLHandler {

	public List<SaturnData> executeQuery(ISaturnSQL saturnSQL,
			Connection conn) throws SaturnComponentException {
		if (conn != null) {
			PreparedStatement ps = null;
			ResultSet rs = null;
			int psIndex = 1;
			try {
				ps = conn.prepareStatement(saturnSQL.getSql());
				debug("QUERY_SQL = " + saturnSQL.getSql());
				psIndex = setConditionPrepareStatement(saturnSQL, psIndex, ps);
				psIndex = setPagePrepareStatement(saturnSQL, psIndex, ps);
				rs = ps.executeQuery();
				ps.clearParameters();
				List<SaturnData> list = setListValueFromRs(saturnSQL, rs);
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				throw new SaturnComponentException("query Exception");
			} finally {
				if (rs != null) {
					try {
						rs.close();
					} catch (SQLException e) {
						e.printStackTrace();
						throw new SaturnComponentException(
								"ResultSet close exception");
					}
				}
				if (ps != null) {
					try {
						ps.close();
					} catch (SQLException e) {
						e.printStackTrace();
						throw new SaturnComponentException(
								"Statment close exception");
					}
				}
			}
		} else {
			throw new SaturnComponentException(
					"Can not get Connection in database component"); //$NON-NLS-1$
		}
	}

	private int setPagePrepareStatement(ISaturnSQL saturnSQL, int psIndex,
			PreparedStatement ps) throws SQLException {

		int i = psIndex;
		int startIndex = saturnSQL.getStartIndex();
		int endIndex = saturnSQL.getEndIndex();
		if (startIndex == 0 && endIndex == 0) {
			return i;
		}
		ps.setInt(i++, startIndex);
		ps.setInt(i++, endIndex);
		return i;
	}

	/**
	 * 将ResultSet中的查询结果封装到list
	 * 
	 * @param data
	 * @param rs
	 * @return
	 * @throws Exception
	 */
	private List<SaturnData> setListValueFromRs(ISaturnSQL saturnSQL, ResultSet rs)
			throws Exception {

		List<SaturnData> list = new ArrayList<SaturnData>();
		String fullKey = saturnSQL.getSaturnDataFullKey();
		IDataInfo dataInfo = SaturnDataUtil.getDataInfo(fullKey);
		int columnCount = rs.getMetaData().getColumnCount();
		ResultSetMetaData rsMetaData = rs.getMetaData();
		List<String> usedKeyList = new ArrayList<String>();
		for (String key : dataInfo.keySet()) {
			for (int i = 0; i < columnCount; i++) {
				if (rsMetaData.getColumnName(i + 1).equalsIgnoreCase(key)) {
					usedKeyList.add(key);
				}
			}
		}
		while (rs.next()) {
			SaturnData saturnData = SaturnDataUtil.getSaturnData(fullKey);
			for (String key : usedKeyList) {
				IDataItem dataItem = dataInfo.getDataItem(key);
				if (dataItem != null) {
					String sqlType = dataItem.getType();
					ResultSetSetUtils.setValues(sqlType, saturnData, key, rs);
				}
			}
			list.add(saturnData);
		}
		debug(Messages.getString("AbstractDatabaseCompononent.43")); //$NON-NLS-1$
		debug(Messages.getString("AbstractDatabaseCompononent.44")); //$NON-NLS-1$
		if (list.size() == 0) {
			return null;
		}
		return list;
	}
	
	@Override
	protected String getSpecialSqlKey(String typeKey) {
		if ("FLOAT".equalsIgnoreCase(typeKey)){
			typeKey = "DOUBLE";
		}
		return typeKey;
	}	
	
	public int executeUpdate(ISaturnSQL saturnSQL, Connection connection)
			throws SaturnComponentException {
		return 0;
	}
}